package com.adams.right.dao.impl;

import java.util.List;

import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.springframework.stereotype.Repository;

import com.adams.access.db.bean.Right;
import com.adams.access.db.dao.hibernate.HibernateDao;
import com.adams.right.dao.RightDao;
import com.adams.utils.Page;

/**
 * 
 * 
 * File Name : RightDaoImpl.java
 * 
 * @Description : 权限管理数据操作接口，提供对权限的增、删、改查功能
 * @author 902990封文进
 */
@Repository("rightDao")
public class RightDaoImpl extends HibernateDao<Right, Long> implements RightDao
{

	public void delete(Right right)
	{
		super.delete(right);

		// 删除角色权限关系
		Session session = getSession();
		SQLQuery sql = session
				.createSQLQuery("delete from t_role_right where right_id="
						+ right.getId());
		sql.executeUpdate();
	}

	/**
	 * 
	 * Description : 根据分页查询所有权限
	 * 
	 * @param page
	 * @return Page<Right>
	 * 
	 */
	public Page<Right> queryAllByPage(Page<Right> page)
	{
		return queryPage(page, "from Right");
	}

	/**
	 * 
	 * Description : 根据权限码查询权限
	 * 
	 * @param code
	 * @return Right
	 * 
	 */
	public Right queryByCode(String code)
	{
		List<Right> list = query("from Right as r where r.rightCode=?", code);
		if (list != null && list.size() > 0)
		{
			return list.get(0);
		}
		return null;
	}

}
